import requests
from lxml import etree
import openpyxl

# 翻页
for kk in range(1, 2):
    url = 'https://www.gongyouhui.com/job/a25?pageNum=' + str(kk)


# 获取首页标题
def new_list():
    alls_href_lists = []
    response = requests.get(url)
    html = etree.HTML(response.text)
    for mm in range(1, 31):
        href_list_s = html.xpath(
            '//*[@id="__layout"]/div/div[3]/div/div[1]/div[1]/div[' + str(
                mm) + ']/div/a/div[1]/div[1]/div[1]/div/text()')
        alls_href_lists.extend(href_list_s)
    return alls_href_lists


# 获取每页href
def scrape_index():
    all_href_list = []
    response = requests.get(url)
    html = etree.HTML(response.text)
    href_list = html.xpath('//body/div/div/div/div/div/div/div/div/div/a/@href')
    all_href_list.extend(href_list)
    return all_href_list


# 获取详情页数据
def scrape_detail(url):
    response = requests.get(url)
    html = etree.HTML(response.text)
    title = html.xpath('//*[@id="__layout"]/div/div[2]/div/div/div[1]/div[1]/div/div[1]/div[1]/div[1]/h1/text()')
    salary = html.xpath('//*[@id="__layout"]/div/div[2]/div/div/div[1]/div[1]/div/div[1]/div[1]/div[2]/text()')
    release_time = html.xpath('//*[@id="__layout"]/div/div[2]/div/div/div[1]/div[1]/div/div[1]/div[2]/p/text()')
    date = release_time[0].split(':')[1]  # 提取第一个字符串中的日期部分
    release_times = date.split(' ')
    # print(release_times[0])
    recruitment_details = html.xpath('//*[@id="__layout"]/div/div[2]/div/div/div[1]/div[2]/div[2]/span/text()')
    location = html.xpath('//*[@id="__layout"]/div/div[2]/div/div/div[1]/div[1]/div/div[2]/div[1]/div[2]/div[2]/text()')
    labels = html.xpath('//*[@id="__layout"]/div/div[2]/div/div/div[1]/div[2]/div[1]/div[1]/text()')
    labels_s = html.xpath('//*[@id="__layout"]/div/div[2]/div/div/div[1]/div[2]/div[1]/div[2]/text()')
    labels_ss = html.xpath('//*[@id="__layout"]/div/div[2]/div/div/div[1]/div[2]/div[1]/div[3]/text()')
    # 异常处理
    if not labels:
        labels = ['']
    if not labels_s:
        labels_s = ['']
    if not labels_ss:
        labels_ss = ['']
    return {
        # '招工标题': title,
        '招工标题': title[0] if title else '',
        '薪资': salary[0] if salary else '',
        '发布时间': release_times[0] if release_times else '',
        '招工详情': recruitment_details[0] if recruitment_details else '',
        '地址': location[0] if location else '',
        '标签': labels[0] if labels else '' + labels_s[0] if labels_s else '' + labels_ss[0] if labels_ss else ''
    }


# 新建表并存入数据
def new_table():
    # 或者新建一个工作簿
    workbook = openpyxl.Workbook()
    # 选择一个工作表
    sheet = workbook.active
    # 创建标题列表
    headers = ['招工标题', '薪资', '发布时间', '招工详情', '地址', '标签']
    sheet.append(headers)  # 将标题添加到工作表中
    # 调用scrape_index方法
    url_list = scrape_index()
    for path in url_list:
        datail = scrape_detail(path)
        # 将数据添加到下一个空行中
        values = list(datail.values())  # 将数据转换为列表
        sheet.append(values)  # 将数据添加到工作表中，紧接在标题之后

    # 保存工作簿
    workbook.save('example.xlsx')


new_table()
